<template>
  <div class="region-list">
    <el-checkbox-group
      v-model="checks"
      @change="handleCheckedAreasChange($event, area)"
    >
      <div
        v-for="area in data"
        :key="area.id"
        class="node-box"
      >
        <checkbox-child
          v-if="area.children && area.children.length"
          :data="area.children"
        />
        <el-checkbox
          v-else
          :label="area.id"
        >
          {{ area.name }}
        </el-checkbox>
      </div>
    </el-checkbox-group>
  </div>
</template>

<script>
// import { explainTreeLeaf } from '@/utils'
export default {
  name: 'CheckboxChild',
  props: {
    titles: {
      type: Array,
      default: () => {
        return ['列表 1', '列表 2']
      }
    },
    data: {
      type: Array,
      default: () => {
        return [
          {
            id: 1,
            name: '华东',
            checks: [],
            checkAll: false,
            isIndeterminate: false,
            children: [
              {
                id: 11,
                name: '上海',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 1
              },
              {
                id: 12,
                name: '江苏',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 1
              },
              {
                id: 13,
                name: '浙江',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 1
              },
              {
                id: 14,
                name: '安徽',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 1
              }
            ]
          },
          {
            id: 2,
            name: '华北',
            checks: [],
            checkAll: false,
            isIndeterminate: false,
            children: [
              {
                id: 21,
                name: '北京',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 2
              },
              {
                id: 22,
                name: '天津',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 2
              },
              {
                id: 23,
                name: '河北',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 2
              },
              {
                id: 24,
                name: '山东',
                checks: [],
                checkAll: false,
                isIndeterminate: false,
                parent_id: 2
              }
            ]
          }
        ]
      }
    }
  },
  data() {
    return {
      checkAll: false,
      checkedAreas: [1],
      cities: ['上海', '北京', '广州', '深圳'],
      isIndeterminate: true
    }
  },
  computed: {
    regions: {
      get() {
        return this.source
      },
      set(v) {
        console.log(v)
      }
    },
    checkNodes() {
      return []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    handleCheckAllChange(val, data) {
      if (val) {
        console.log(val)
        data.checks = data.children.map(item => {
          return item.id
        })
      } else {
        data.checks = []
      }
      console.log(data)
      this.isIndeterminate = false
    },
    handleCheckedAreasChange(value, data) {
      const checkedCount = value.length
      data.checkAll = checkedCount === data.children.length
      data.isIndeterminate =
        checkedCount > 0 && checkedCount < data.children.length
    }
  }
}
</script>
<style lang="scss" scoped>
.region {
  display: flex;
  &-head {
    flex: 1;
  }
  &-content {
    flex: 3;
  }
}
</style>

